home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7471 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: pointers
  5. Date: 25 Feb 1996 21:08:51 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb25140851@qcd.lanl.gov>
  8. References: <4gqh8g$bo4@news.bu.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: lachesis@cs.bu.edu's message of 25 Feb 1996 20:36:32 GMT
  13.  
  14. In article <4gqh8g$bo4@news.bu.edu>
  15. lachesis@cs.bu.edu (wai yip) writes:
  16.  
  17. wy: can someone who knows a lot about pointers help me with this
  18.  
  19. I never claim to know a lot about anything. I will try to help you
  20. anyway. However, remember asking questions in a newsgroup is no
  21. alternative to actually studying a language. I will suggest you read a
  22. good C text book, the FAQ can guide you with examples of good books.
  23.  
  24. wy: 
  25. wy: int i=3,*p;
  26. wy: 
  27. wy: with the above declaration, what would the bottom lines do?
  28. wy: *p=&i;
  29.  
  30. Compilation error. &i is a pointer to int, *p is an int. A pointer to
  31. int cannot be converted to an int without a cast. With a cast, whether
  32. it is allowed is upto the implementation (which must document it). It
  33. leads to undefined behaviour in addition, see the third case below.
  34.  
  35. Note that compilation error means that the compiler is required to
  36. give a diagnostic of some sort. What it does after giving the
  37. diagnostic is upto it. 
  38.  
  39. wy: p=i;
  40.  
  41. Compilation error. p is a pointer to an int, i is an int. An int
  42. cannot be assigned to a pointer without a cast.
  43.  
  44. wy: *p=i;
  45.  
  46. Undefined behaviour (which means virtually anything can happen, with
  47. or without compiler diagnostic). *p tries to dereference p which has
  48. not been assigned yet.
  49.  
  50. If p has been assigned a valid pointer to an object, this will set
  51. that object to the current value of i, which is 3.
  52.  
  53. wy: p=&i;
  54.  
  55. It assigns the address of i to p. After this, unless p is changed, *p
  56. refers to the object i. 
  57.  
  58. wy: 
  59. wy: please answer me through email because i don't usually read newsgroups.
  60.  
  61. Sorry.
  62.  
  63. Cheers
  64. Tanmoy
  65. --
  66. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  67. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  68. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  69. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  70. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  71. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  72.